package cn.study.luckyinfrastructure.gateway.iml;

import cm.study.config.enums.LdExeceptionEnum;
import cm.study.config.util.AssertUtil;
import cn.study.luckyclient.dto.query.AwardListByParamQuery;
import cn.study.luckydomain.User.Password;
import cn.study.luckydomain.award.AwardEntity;
import cn.study.luckydomain.gateway.AwardGateway;
import cn.study.luckyinfrastructure.convertor.AwardConvertor;
import cn.study.luckyinfrastructure.gateway.iml.dataObject.AwardDB;
import cn.study.luckyinfrastructure.gateway.iml.mapper.AwardMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Objects;

@AllArgsConstructor
@Component
public class AwardGatewayImpl implements AwardGateway {
    private final AwardMapper awardMapper;
    @Override
    public AwardEntity save(AwardEntity awardEntity) {
        AwardDB awardDB = AwardConvertor.toAwardDB(awardEntity);
        if(Objects.isNull(awardEntity.getId())){
            int insert = awardMapper.insert(awardDB);
            AssertUtil.isTrue(insert<1, LdExeceptionEnum.ADD_ERROR.getDescription());
        }
        else{
            int i = awardMapper.updateById(awardDB);
            AssertUtil.isTrue(i<1,LdExeceptionEnum.UPDATE_ERROR.getDescription());
        }
        return AwardConvertor.toEntity(awardDB);
    }

    @Override
   public IPage<AwardEntity> page(AwardListByParamQuery awardListByParamQuery) {
           IPage<AwardDB> page=awardMapper.page(new Page<AwardDB>(awardListByParamQuery.getPageIndex(),awardListByParamQuery.getPageSize()),awardListByParamQuery);
            return page.convert(AwardConvertor::toEntity);
    }

    @Override
    public int deducation(Long id, int i) {
        return awardMapper.deucation(id,i);
    }
}
